package com.yyk.yykywd.mvvm.ui

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.mvvm.adapter.DynamicBroadCastAdapter1
import com.yyk.yykywd.mvvm.entity.DynamicBroadcastBean
import com.yyk.yykywd.mvvm.view_model.DynamicBroadcastViewModel
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_dynamic_broadcast.*
import kotlinx.android.synthetic.main.activity_dynamic_broadcast.loading_layout
import kotlinx.android.synthetic.main.layout_load.*

class DynamicBroadcastActivity : BaseActivity() {
    private lateinit var viewModel: DynamicBroadcastViewModel
    private lateinit var adapter: DynamicBroadCastAdapter1
    override fun initData() {
        viewModel.getReport()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_dynamic_broadcast
    }

    override fun initView() {
        setBackArrow()
        setTitle("动态播报")
        loading_layout.showLoading()

        viewModel = getViewModel(this, DynamicBroadcastViewModel::class.java)
        viewModel.id = intent.getStringExtra(ID)

        rv.layoutManager = LinearLayoutManager(mActivity)
        adapter = DynamicBroadCastAdapter1(mActivity, R.layout.item_dynamic_broadcast, viewModel.reports)
        rv.adapter = adapter

        viewModel.dynamicBroadcast.observe(this,
                Observer<DynamicBroadcastBean> {
                    hideRefreshAndLoadMore(srl)
                    showStateLayout(adapter, it.code, viewModel.reports, it.msg, loading_layout)
                })
    }

    override fun setListener() {
        super.setListener()
        srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
                viewModel.getReport()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh(viewModel.reports)
                viewModel.getReport()
            }

        })
    }

}
